package com.pan.hisystem.controller;


import com.pan.hisystem.pojo.Outpatients;
import com.pan.hisystem.pojo.PatientTest;
import com.pan.hisystem.pojo.RespBean;
import com.pan.hisystem.pojo.TestImage;
import com.pan.hisystem.service.IOutpatientsService;
import com.pan.hisystem.service.IPatientTestService;
import com.pan.hisystem.service.ITestImageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qingyang
 * @since 2022-04-02
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private IPatientTestService patientTestService;
    @Autowired
    private IOutpatientsService outpatientsService;
    @Autowired
    private ITestImageService testImageService;

    @ApiOperation(value = "根据卡号获取患者化验项目")
    @GetMapping("/getTestMesByMid")
    public List<Outpatients> getTestMesByMid(String mid){
        return outpatientsService.getTestByMid(mid);
    }

    @ApiOperation(value = "完成化验")
    @PutMapping("/updateTestResult")
    public RespBean updateTestResult(@RequestBody PatientTest patientTest){
        System.out.println(patientTest.toString());
        if(patientTest.getId()==null){
            return RespBean.error("更新失败!");
        }
        if(!patientTest.getPayStatus()){
            return RespBean.error("该化验项目未缴费,请先前往缴费!");
        }
        patientTest.setTestTime(LocalDateTime.now());
        patientTest.setStatus(true);
        if(patientTestService.updateById(patientTest)){
            return RespBean.success("更新成功!");
        }
        return RespBean.error("更新失败!");
    }

    @ApiOperation(value = "保存化验图片")
    @GetMapping("/addUrls")
    public RespBean addUrls(@RequestParam(value = "urls") String[] urls,Integer tid){
        List<TestImage> list=new ArrayList<>();
        for(int i=0;i<urls.length;i++){
            if(urls[i]!=null){
                TestImage testImage=new TestImage();
                testImage.setImageURL(urls[i]);
                testImage.setTid(tid);
                list.add(testImage);
            }
        }
        if(testImageService.saveBatch(list)){
            return RespBean.success("添加成功!");
        }
        return RespBean.error("添加失败!");
    }

}
